package com.patrol.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patrol.dao.*;
import com.patrol.pojo.*;
import com.patrol.service.WorkerService;
import com.patrol.utils.DownloadSettingUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class WorkerServiceImpl implements WorkerService {
    @Resource
    private WorkDao workDao;

    @Resource
    private RiskPointDao riskPointDao;

    @Resource
    private TaskPointDao taskPointDao;

    @Resource
    private XgexportDao xgexportDao;

    @Resource
    private RwexportDao rwexportDao;

    @Resource
    private UserkqDao userkqDao;

    @Override
    public boolean insert(Worker worker) {
        return 1 == workDao.insert(worker);
    }

    @Override
    public boolean delete(int workerId) {
        return 1 == workDao.deleteById(workerId);
    }

    @Override
    public boolean update(Worker worker) {
        return 1 == workDao.updateById(worker);
    }

    @Override
    public Worker select(int id) {
        return workDao.selectById(id);
    }

    @Override
    public Page<Worker> selectPage(long currentPage, long limit) {
        return workDao.selectPage(new Page<>(currentPage, limit), null);
    }

    @Override
    public void exportDataByDate(HttpServletResponse response, String startTime, String endTime) throws IOException {
        DownloadSettingUtil.setting(response, "worker-data");
        QueryWrapper<Worker> qw = new QueryWrapper<>();
        qw.between("date", startTime, endTime);
        List<Worker> list = workDao.selectList(qw);
        EasyExcel.write(response.getOutputStream(), Worker.class).sheet("data").doWrite(list);
    }

//    @Override
//    public boolean signWork(String userId, String date) {
//        int i = workDao.signWork(Integer.parseInt(userId),date);
//        return i==1;
//    }

    @Override
    public List<Worker> selectAllWorkers() {
        return workDao.selectAllWorkers();
    }

    @Override
    public void exportAllRiskPoints(HttpServletResponse response) throws IOException {
        String filename = "RiskPoint-Information";
        DownloadSettingUtil.setting(response, filename);
        List<RiskPoint> list = riskPointDao.selectAllRiskPoints();
        for (int i=0;i<list.size();i++){
            RiskPoint riskPoint = list.get(i);
            // 风险点位的等级，0：高风险  1：中风险   2：低风险
            if (riskPoint.getStatus().equals("0")){
                riskPoint.setStatus("高风险");
            }else if (riskPoint.getStatus().equals("1")){
                riskPoint.setStatus("中风险");
            }else {
                riskPoint.setStatus("低风险");
            }
        }
        EasyExcel.write(response.getOutputStream(), RiskPointVO.class).sheet("风险点位信息").doWrite(list);
    }

    @Override
    public void exportAllTaskPoints(HttpServletResponse response) throws IOException {
        String filename = "TaskPoint-Information";
        DownloadSettingUtil.setting(response, filename);
        List<TaskPoint> list = taskPointDao.selectList(null);
        List<TaskPointVO> list1 = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            TaskPointVO taskPointVO = new TaskPointVO();
            taskPointVO.setTaskId(list.get(i).getTaskId());
            taskPointVO.setTaskName(list.get(i).getTaskName());
            taskPointVO.setTaskAddress(list.get(i).getTaskAddress());
            taskPointVO.setTaskInfo(list.get(i).getTaskInfo());
            taskPointVO.setTaskLatitudeX(list.get(i).getTaskLatitudeX());
            taskPointVO.setTaskLongitudeY(list.get(i).getTaskLongitudeY());
            taskPointVO.setFrequency(list.get(i).getFrequency());
            taskPointVO.setCreateTime(list.get(i).getCreateTime());
            taskPointVO.setUpdateTime(list.get(i).getUpdateTime());
            List<String> realNames = taskPointDao.selectUUPoint(list.get(i).getTaskId());
            StringBuffer sb = new StringBuffer();
            for (int j=0;j<realNames.size();j++){
                sb.append(realNames.get(j) + "、");
            }
            String s = sb.toString();
            taskPointVO.setRealName(s);
            list1.add(taskPointVO);
        }
        EasyExcel.write(response.getOutputStream(), TaskPointVO.class).sheet("任务点位信息").doWrite(list1);
    }

    @Override
    public void exportAllLCTaskPoints(HttpServletResponse response) throws IOException {
        String filename = "LCTaskPoint";
        DownloadSettingUtil.setting(response, filename);

//        EasyExcel.write(response.getOutputStream(), TaskPointVO.class).sheet("漏查任务点").doWrite();
    }

    @Override
    public void userClock(Integer userId, String time) {
        workDao.userClock(userId,time);
    }

    @Override
    public Integer findUserClock(Integer userId, String time) {
        Integer userClock = workDao.findUserClock(userId, time);
        return userClock;
    }

    @Override
    public List<String> findHistoryUserClock(Integer userId) {
        List<String> historyUserClock = workDao.findHistoryUserClock(userId);
        return historyUserClock;
    }

    @Override
    public void createUserTask(Integer userId) {
        workDao.createUserTask(userId);
    }

    @Override
    public void updateUserTaskNum(Integer userId) {
        workDao.updateUserTaskNum(userId);
    }

    @Override
    public void updateDQTaskSign(String userId) {
        workDao.updateTaskSign(Integer.parseInt(userId));
    }

    @Override
    public List<SendInfo> selectInfo(Integer userId) {
        List<SendInfo> sendInfoList = workDao.selectInfo(userId);
        return sendInfoList;
    }

    @Override
    public List<Integer> selectNowXGUser() {
        List<Integer> list = workDao.selectNowXGUser();
        return list;
    }

    @Override
    public void updateXGLXSign(Integer userId) {
        workDao.updateXGLXSign(userId);
    }

    @Override
    public String selectRWOverTime(String userId) {
        return workDao.selectRWOverTime(Integer.parseInt(userId));
    }

    @Override
    public List<Xglx> selectAllInRW(String userId) {
        return workDao.selectAllInRW(Integer.parseInt(userId));
    }

    @Override
    public List<Xgexport> XGUserNumExport(String startTime, String endTime) {
        QueryWrapper<Xgexport> qw = new QueryWrapper<>();
        qw.between("time", startTime, endTime);
        return xgexportDao.selectList(qw);
    }

    @Override
    public void addXGTJ(int xgzs,int dgzs,String time) {
        workDao.addXGTJ(xgzs,dgzs,time);
    }

    @Override
    public void updateXGTJ(int id,int dgzs) {
        workDao.updateXGTJ(id,dgzs);
    }

    @Override
    public Integer selectXGExport(String time) {
        Integer id = workDao.selectXGExport(time);
        return id;
    }

    @Override
    public List<Xgexport> XGUserNumExportQC() {
        List<Xgexport> xgexports = xgexportDao.selectList(null);
        return xgexports;
    }

    @Override
    public List<Rwexport> RWNumExportQC() {
        List<Rwexport> rwexports = rwexportDao.selectList(null);
        return rwexports;
    }

    @Override
    public int selectRWNum() {
        return workDao.selectRWNum();
    }

    @Override
    public int selectRWLCNum() {
        return workDao.selectRWLCNum();
    }

    @Override
    public void addRWTJ(int rwNum, int rwlcNum, String time, int hfxnum, int zfxnum, int dfxnum) {
        workDao.addRWTJ(rwNum,rwlcNum,time,hfxnum,zfxnum,dfxnum);
    }

    @Override
    public void updateRWTJ(Integer id, int rwlcNum, int hfxnum, int zfxnum, int dfxnum) {
        workDao.updateRWTJ(id,rwlcNum,hfxnum,zfxnum,dfxnum);
    }

    @Override
    public Integer selectRWReport(String time) {
        return workDao.selectRWReport(time);
    }

    @Override
    public List<Rwexport> RWNumExport(String startTime, String endTime) {
        QueryWrapper<Rwexport> qw = new QueryWrapper<>();
        qw.between("time", startTime, endTime);
        return rwexportDao.selectList(qw);
    }

    @Override
    public int selectHfxNum() {
        return workDao.selectHfxNum();
    }

    @Override
    public int selectZfxNum() {
        return workDao.selectZfxNum();
    }

    @Override
    public int selectDfxNum() {
        return workDao.selectDfxNum();
    }

    @Override
    public void addUserKQTJ(int userId, String realName, String time, String detailTime) {
        workDao.addUserKQTJ(userId,realName,time,detailTime);
    }

    @Override
    public int selectRWLCNumById(Integer userId) {
        return workDao.selectRWLCNumById(userId);
    }

    @Override
    public void updateUserKQTJ(int userId, int num) {
        workDao.updateUserKQTJ(userId, num);
    }

    @Override
    public List<Userkq> userKQExportQC() {
        return userkqDao.selectList(null);
    }

    @Override
    public List<Userkq> userKQExport(String startTime, String endTime) {
        QueryWrapper<Userkq> qw = new QueryWrapper<>();
        qw.between("dktime", startTime, endTime);
        return userkqDao.selectList(qw);
    }

    @Override
    public int selectXGDGNum(String time) {
        return workDao.selectDGNum(time);
    }

    @Override
    public Integer selectUserToday(Integer userId, String dkTime) {
        return workDao.selectUserToday(userId,dkTime);
    }
}